package calao;

import static calao.util.Logger.*;

import java.io.File;
import java.util.Vector;

class ServerConfig 
{
		
	public final static int DEFAULT_PORT = 7080;
	
	int port;
	
	Vector<Application> appList = new Vector<Application>();

	File desktopsDir = new File(System.getProperty("calao.desktopsDir", "gwt"));
	
	String defaultDesktop = System.getProperty("calao.desktop", "calao.Desktop");
	
	boolean demonize = false;

	ServerConfig() 
	{
		String port = System.getProperty("calao.port");
		this.port = port!=null? Integer.parseInt(port) : DEFAULT_PORT;
	}
	
	ServerConfig(int port)
	{
		this.port = port;
	}
	
	void addApplication(String appName) 
	{
		try {
			appList.add((Application)Class.forName(appName).newInstance());
			log(DEBUG, "Application %s loaded", appName);
		}
		catch (Exception e) {
			log(e);
			log(ERROR, "Cannot load application %s", appName);
		}
	}

	void parseArg(String argSwitch, String value)
	{
		if (argSwitch.equals("-app")) {
			addApplication(value);
		}
		else if (argSwitch.equals("-desktop")) {
			defaultDesktop = value;
			log(INFO, "Default desktop set to %s", defaultDesktop);
		}
		else if (argSwitch.equals("-port")) {
			port = Integer.parseInt(value);
		}	
		else if (argSwitch.equals("-demonize")) {
			demonize = Boolean.parseBoolean(value);
		}	
	}

}
